import { useReactive } from "ahooks";
import { useModel } from "umi";
import compress from "gql-compress";
import { message, Modal } from "antd";

const useCreateSystemUser = () => {
    const { graphqlFetch, remoteData } = useModel("global");

    const state = useReactive({
        loading: false,
    });

    const createSystemUser = async (record, callback) => {
        if (!record.osPhon) return message.warning("没有手机号，不能创建系统用户");
        // 如果手机号已注册，则不能创建系统用户，由手机号注册的用户自行绑定
        Modal.confirm({
            title: "确认",
            content: "确认要创建系统用户吗？",
            okButtonProps: { loading: state.loading },
            onOk: async () => {
                state.loading = true;
                let res = await remoteData({
                    object: "base-platform-management-service",
                    serviceName: "/person/registerFromOrganizationStaff",
                    params: {
                        oiUuid: window.organizationUuid,
                        phone: record.osPhon,
                        realName: record.osName,
                        nation: record.osNati,
                        sex: record.osRoge,
                        birthday: record.osBirt,
                        identityCardNumber: record.osIdnu,
                    },
                });
                if (res) {
                    let variables = {
                        omUuid: res.uuid,
                        omName: res.realName,
                        omAvta: res.avatarHash,
                        omBirt: res.birthday,
                        omJoti: new Date().getUTCTime(),
                        omNati: res.nation,
                        omRoge: res.sex,
                        omPhon: res.phone,

                        osUuid: record.osUuid,
                        osFrom: "来自成员",
                    };

                    res = await graphqlFetch({
                        query,
                        variables,
                    });

                    res && callback && callback(res);
                }
                state.loading = false;
            },
        });
    };

    return { ...state, createSystemUser };
};

let query = compress(`mutation MyMutation(
    $omUuid: UUID!
    $omName: String!
    $omAvta: String
    $omBirt: Date
    $omJoti: BigInt
    $omNati: String
    $omRoge: String
    $omPhon: String

    $osUuid: UUID!
    $osFrom: String!
) {
    createOrganizationMember(
        input: {
            organizationMember: {
                omUuid: $omUuid
                omName: $omName
                omAust: "已认证"
                omAvta: $omAvta
                omBirt: $omBirt
                omCaus: "员工创建"
                omIfim: true
                omJoti: $omJoti
                omNati: $omNati
                omRoge: $omRoge
                omPhon: $omPhon
            }
        }
    ) {
        clientMutationId
    }

    updateOrganizationStaffByOsUuid(
        input: {
            osUuid: $osUuid
            organizationStaffPatch: {
                omUuid: $omUuid,
                osFrom: $osFrom
            }
        }
    ) {
        clientMutationId
    }
}`);

export default useCreateSystemUser;
